﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class NewsCateGoryBll : INewsCateGoryBll
    {
        private INewsCategoryDal _ncate;
        private IMapper _mapper;
        public NewsCateGoryBll(INewsCategoryDal ncate, IMapper mapper)
        {
            _ncate = ncate;
            _mapper = mapper;
        }

        public async Task<int> AddNewsCateGoryAsync(string title)
        {
            return await _ncate.AddAsync(new Newscategory 
            {
                Title = title
            });
        }

        public async Task<int> DeleteNewsCateGoryAsync(int id)
        {
            var data = await _ncate.QueryAsync(id);
            return data == null ? -1 : await _ncate.DeleteAsync(data);
        }

        public async Task<int> EditNewsCateGoryAsync(int id, string title)
        {
            var data = await _ncate.QueryAsync(id);
            if (data == null)
                return -1;
            data.Title = title;
            return await _ncate.EditAsync(data);
        }

        public async Task<List<NewsCategoryDto>> GetNewsCateGoryAllAsync()
        {
            var data = await _ncate.QueryAsync();
            return _mapper.Map<List<Newscategory>,List<NewsCategoryDto>>(data);
        }

        public async Task<NewsCategoryDto> QueryNewsCateGoryByIdAsync(int id)
        {
            var data = await _ncate.QueryAsync(id);
            return _mapper.Map<Newscategory, NewsCategoryDto>(data);
        }
    }
}
